﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Mvc;
using NHibernate;
using SimpleScrum.Areas.Scrum.Models;
using SimpleScrum.Core.Entities;
using SimpleScrum.Helpers;
using SimpleScrum.Utils;

namespace SimpleScrum.Areas.Scrum.Controllers
{
    public class BackLogController : Controller
    {
        public BackLogModel ObjBackLog;
        public ProjectModel ObjProject;
        public SprintModel objsprint;
        public UserStoryModel objuserstory;
        private ISession _session = NHibernateHelper.GetSession();

        public BackLogController()
        {
            ObjBackLog = new BackLogModel();
            ObjProject = new ProjectModel();
            objsprint= new SprintModel();
            objuserstory=new UserStoryModel();
        }

        // 
        // GET: /BackLog/ 
        [HttpGet]
        public ActionResult Index()
        {
            var model = new UserStorySprintModel()
                {
                    BackLogList = ObjBackLog.GetBackLogs(Client.Instance.CurrentProjectId),
                    SprintList = objsprint.GetSprints(Client.Instance.CurrentProjectId),
                    UserStoryList = objuserstory.GetUserStorys(Client.Instance.CurrentProjectId)

                    
                };
            //IList<BackLog> backLogs = ObjBackLog.GetBackLogs(Client.Instance.CurrentProjectId);
           
            return View(model);
        }
        [HttpPost]
        public ActionResult Index(Guid id)
        {

            var model = new UserStorySprintModel()
            {
                BackLogList = ObjBackLog.GetBackLogs(Client.Instance.CurrentProjectId),
                SprintList = objsprint.GetSprints(Client.Instance.CurrentProjectId),
                UserStoryList = objuserstory.GetUserStorys(Client.Instance.CurrentProjectId)


            };
            //IList<BackLog> backLogs = ObjBackLog.GetBackLogs(Client.Instance.CurrentProjectId);

            return View(model);
        }

        // 
        // GET: /BackLog/Details/
        [HttpGet]
        public ActionResult Details(Guid id)
        {
            BackLog backLog = ObjBackLog.GetBackLogById(id);
            return View(backLog);
        }
       
        
        // 
        // GET: /BackLog/Create 
        [HttpGet]
        public ActionResult New()
        {
            var backLog = new BackLog();
            return View(backLog);
        }

        // 
        // POST: /BackLog/Create 
        [HttpPost]
        public ActionResult New(BackLog backLog)
        {
            try
            {
                backLog.Project = ObjProject.GetProjectById(Client.Instance.DefaultProjectId);
                ObjBackLog.NewBackLog(backLog);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // 
        // GET: /BackLog/Edit/5 
        [HttpGet]
        public ActionResult Edit(Guid id)
        {
            BackLog backLog = ObjBackLog.GetBackLogById(id);
            return View(backLog);
        }

        // 
        // POST: /BackLog/Edit/5 
        [HttpPost]
        public ActionResult Edit(Guid id, BackLog backLog)
        {
            try
            {
                ObjBackLog.UpdateBackLog(backLog);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // 
        // GET: /BackLog/Delete/5 
        [HttpGet]
        public ActionResult Delete(Guid id)
        {
            BackLog backLog = ObjBackLog.GetBackLogById(id);
            return View(backLog);
        }

        // 
        // POST: /BackLog/Delete/5 
        [HttpPost]
        public ActionResult Delete(Guid id, FormCollection collection)
        {
            try
            {
                BackLog backLog = ObjBackLog.GetBackLogById(id);
                ObjBackLog.DeleteBackLog(backLog);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }
        }
    }
}